package com.example.springLearning.part7.service;

import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Service
public class Fixed2ElectricService {

    /**
     * 不添加lazy注解会造成循环依赖的问题，解决办法有两种
     * 1.在添加的依赖上加上lazy注解
     * 2.使用SpringContentHolder获取已存在的bean
     */

    @Autowired
    private ApplicationContext context;

    Fixed2ElectricService fixed2ElectricService;

    public void charge() throws Exception {
        System.out.println("Electric charging ...");
//        fixed2ElectricService = context.getBean(Fixed2ElectricService.class);
        fixed2ElectricService = (Fixed2ElectricService) AopContext.currentProxy();
        fixed2ElectricService.pay();
    }

    public void pay() throws Exception {
        System.out.println("Pay with alipay ...");
        Thread.sleep(1000);
    }
}
